<?php
/**
 * IG_View_Helper_JqueryPaginator
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: JqueryPaginator.php 524 2011-05-24 01:44:20Z mmxie $
 */
class IG_View_Helper_JqueryPaginator extends Zend_View_Helper_Abstract
{
    protected $_scrollingStyle = 'Sliding';

    protected $_defautlFirstPageTitle = '首页';
    protected $_defaultPreviousPageTitle = '上一页';
    protected $_defaultNextPageTitle = '下一页';
    protected $_defaultLastPageTitle = '尾页';


    //JQuery ajax link options
    protected $_ajaxOptions = array('inline' => true, 'noscript' => true);

    /**
     * @return string
     */
    public function getScrollingStyle()
    {
        return $this->_scrollingStyle;
    }

    /**
     * @param string $scrollingStyle
     */
    public function setScrollingStyle($scrollingStyle)
    {
        $this->_scrollingStyle = $scrollingStyle;
    }

    public function getAjaxOptions()
    {
        return $this->_ajaxOptions;
    }
    /**
     *
     * @param array $ajaxOptions
     */
    public function setAjaxOptions($ajaxOptions)
    {
        $this->_ajaxOptions = array_merge($this->_ajaxOptions, $ajaxOptions);
    }

    /**
     *
     * @param $paginator
     * @param $ajaxOptions
     * @param $params
     */
    public function jqueryPaginator(Zend_Paginator $paginator, $ajaxOptions = null, $params = null)
    {
        if ($paginator === null) {
            if (isset($this->view->paginator) and $this->view->paginator !== null and $this->view->paginator instanceof Zend_Paginator) {
                $paginator = $this->view->paginator;
            } else {
                /**
                 * @see Zend_View_Exception
                 */
                require_once 'Zend/View/Exception.php';

                $e = new Zend_View_Exception('No paginator instance provided or incorrect type');
                $e->setView($this->view);
                throw $e;
            }
        }

        if ($ajaxOptions !== null) {
            $this->setAjaxOptions($ajaxOptions);
        }

        $pages = get_object_vars($paginator->getPages($this->_scrollingStyle));

        if ($params !== null) {
            $pages = array_merge($pages, (array) $params);
        } else {
            $pages = array_merge($pages, array('params' => array()));
        }
        if (!array_key_exists('previous', $pages)) {
            $pages['previous'] = 1;
        }
        if (!array_key_exists('next', $pages)) {
            $pages['next'] = $pages['last'];
        }
        $pagesLink = array();
        $ajaxOptions = $this->getAjaxOptions();

        $pagesLink[] = $this->view->ajaxLink($this->_defautlFirstPageTitle,
                $this->view->url(array_merge(array('page' => $pages['first']), $pages['params'])), $ajaxOptions);
        $pagesLink[] = $this->view->ajaxLink($this->_defaultPreviousPageTitle,
                $this->view->url(array_merge(array('page' => $pages['previous']), $pages['params'])), $ajaxOptions);

        //Numbered page links
        foreach ($pages['pagesInRange'] as $page) {
            if ($page != $pages['current']) {
                $pagesLink[] = $this->view->ajaxLink($page,
                  $this->view->url(array_merge(array('page' => $page), $pages['params'])), $ajaxOptions);
            } else {
                $pagesLink[] = '<span class="current">' . $page . '</span>';
            }
        }

        //Next page link
        $pagesLink[] = $this->view->ajaxLink($this->_defaultNextPageTitle,
                $this->view->url(array_merge(array('page' => $pages['next']), $pages['params'])), $ajaxOptions);
        $pagesLink[] = $this->view->ajaxLink($this->_defaultLastPageTitle,
                $this->view->url(array_merge(array('page' => $pages['last']), $pages['params'])), $ajaxOptions);

        $xhtml = implode($pagesLink, ' | ');

        return $xhtml;
    }
}